<?php
    class mod_point extends CI_Controller{
        function  __construct(){
            parent::__construct();
        }  
        function index(){
            $dat['point']=$this->common_model->getone('point');
            $id=$this->session->userdata('id');
            $dat['url']=base_url().'view-site/'.md5('vs').$id;
            $dat['user']=$this->common_model->getone('users',array('id'=>$id));
            $data['content']=$this->load->view('mod_point/url',$dat,true);
            $this->load->view('index',$data);
        }
        
        function viewsite(){
            $ip=$_SERVER["REMOTE_ADDR"];
            $check=$this->common_model->getone('ipaddress',array('ip'=>$ip));
            //$check=1;
            if(empty($check)){
                $offset=$this->uri->segment(2);
                $id=substr($offset,32);
                $user=$this->common_model->getone('users',array('id'=>$id));
                $diem=$this->common_model->getone('point')->point_url;
                $point=$user->point;
                $this->common_model->updatedata('users',array('id'=>$id),array('point'=>$point+$diem));
                $this->common_model->insertdata('ipaddress',array('ip'=>$ip));
                redirect(base_url());
            }else{
                redirect(base_url());
            }
        }
        
        function exchange(){
			$gift_id = $this->uri->segment(2);
        	$this->load->library('form_validation');
			$this->form_validation->set_rules('name', 'Tên ','required|xss_clean');
			$this->form_validation->set_rules('address', 'Địa chỉ','required|xss_clean');
			$this->form_validation->set_rules('birthday', 'Ngày sinh','required|xss_clean');
			$this->form_validation->set_rules('province', 'Tỉnh/Thành phố','required|xss_clean');
			$this->form_validation->set_rules('district', 'Quận/Huyện','required|xss_clean');
			$this->form_validation->set_rules('birthday', 'Ngày sinh','required|xss_clean');
			$this->form_validation->set_rules('phone', 'Điện thoại','numeric|required|xss_clean');
			$this->form_validation->set_rules('checkbox', 'Bạn phải xác nhận thông tin','required');
			$this->form_validation->set_error_delimiters('<p class="roww"><label class="title"></label><i class="icon-remove icon-1x" style="color:red;padding:0 5px"></i>','</p>');
			$dat['user']=$this->common_model->getone('users',array('id'=>$this->session->userdata('id')));
        	$dat['gift']=$this->common_model->getone('gift', array('id'=>$gift_id));
        	if($this->form_validation->run()==FALSE || $this->input->post('checkbox')!=1){
			}
			else{
				$order_date = date("d/m/Y",time());
				if($dat['gift'] == 0){
					$point = $dat['user']->point - $dat['gift']->cost;
				}
				else{
					$point = $dat['user']->point;
				}
				$this->common_model->insertdata('exchange',array('user_id'=>$this->session->userdata('id'),'gift_id'=>$this->input->post('gift_id'),'name'=>$this->input->post('name'),'birthday'=>$this->input->post('birthday'),'address'=>$this->input->post('address'),'province'=>$this->input->post('province'),'district'=>$this->input->post('district'),'phone'=>$this->input->post('phone'),'order_date'=>$order_date));
				$this->common_model->updatedata('users', array('id'=>$this->session->userdata('id')), array('point'=>$point));
				redirect(base_url().'doi-thanh-cong.html');
			}
			$dat['point']=$this->common_model->getone('point');
			$dat['error'] = false;
			if($dat['user']->point < $dat['gift']->cost){
				$dat['error'] = true;
			}
			$data['content']=$this->load->view('mod_point/exchange',$dat,true);
			$this->load->view('index',$data);
        }
		
		function my_gift(){
        	$dat['user']=$this->common_model->getone('users',array('id'=>$this->session->userdata('id')));
        	$dat['exchange']=$this->common_model->getdata('exchange', array('user_id'=>$dat['user']->id));
			$data['content']=$this->load->view('mod_point/my_gift',$dat,true);
			$this->load->view('index',$data);
		}
		
		function exchange_success(){
        	$dat['user']=$this->common_model->getone('users',array('id'=>$this->session->userdata('id')));
			$data['content']=$this->load->view('mod_point/exchange_success',$dat,true);
			$this->load->view('index',$data);
		}
    }
